#!/bin/bash
#2017年6月14日21:27:31
#auto config httpd vhosts
#by author jfedu.net
#####################
APACHE_SOFT="httpd httpd-devel httpd-tools"
BACK_DIR=/data/backup/`date +%F`
HTTP_DIR="/etc/httpd/conf"
HTTP_FILES="httpd.conf"
VHOSTS_CONF="vhosts.conf"
NUM1=$(grep -c "$VHOSTS_CONF" $HTTP_DIR/$HTTP_FILES)
NUM2=$(grep -c "NameVirtualHost" $HTTP_DIR/$VHOSTS_CONF)
DOMAIN="$1"

if [ $# -eq 0 ];then
	echo -e "\033[32m-----------------\033[0m"
	echo -e "\033[32mUsage:{Please Enter sh $0 www.jf1.com|www.jf2.com}\033[0m"
	exit 0
fi

yum install $APACHE_SOFT -y
mkdir  -p  $BACK_DIR
cp  -a  $HTTP_DIR/$HTTP_FILES $BACK_DIR
touch $HTTP_DIR/$VHOSTS_CONF

if [ -z $NUM1 ];then
	NUM1=0
fi
if [ $NUM1 -eq 0 ];then
	echo "Include  conf/$VHOSTS_CONF" >>$HTTP_DIR/$HTTP_FILES
fi

if [ -z $NUM2 ];then
	NUM2=0
fi
if [ $NUM2 -eq 0 ];then
	echo "NameVirtualHost *:80" >>$HTTP_DIR/$VHOSTS_CONF
fi

NUM3=`grep -c "$DOMAIN" /etc/httpd/conf/vhosts.conf`
if [ $NUM3 -eq 0 ];then
echo "
<VirtualHost *:80>
    ServerAdmin wgkgood@163.com
    DocumentRoot  \"/data/webapps/$DOMAIN\"
    ServerName  $DOMAIN
  <Directory \"/data/webapps/$DOMAIN\">
    AllowOverride All
    Options -Indexes FollowSymLinks
    Order allow,deny
    Allow from all
  </Directory>
    ErrorLog  logs/error_log
    CustomLog logs/access_log common
</VirtualHost>
" >>$HTTP_DIR/$VHOSTS_CONF
fi
